package com.tsing.bms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.entity.Building;
import com.tsing.bms.mapper.BuildingMapper;
import com.tsing.bms.service.IBuildingService;
import com.tsing.bms.vo.req.BuildingAddReqVo;
import com.tsing.bms.vo.req.BuildingUpdateReqVo;
import com.tsing.bms.vo.req.IdReqVo;
import com.tsing.bms.vo.req.IdsReqVo;
import com.tsing.bms.vo.resp.BuildingRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import com.tsing.common.utils.BeanCoperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Override
    public void addBuilding(BuildingAddReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Building>().eq("name", reqVo.getName()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Building building = new Building();
        BeanUtils.copyProperties(reqVo, building);
        baseMapper.insert(building);
    }

    @Override
    public void deleteBuilding(IdsReqVo reqVo) {
        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
            throw new BusinessException(ServerException.Proxy.BUILDING_HAVE_RELEVANCE);
        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public BuildingRespVo getBuildingById(IdReqVo reqVo) {
        BuildingRespVo buildingRespVo = new BuildingRespVo();
        BeanUtils.copyProperties(baseMapper.selectById(reqVo.getId()), buildingRespVo);
        return buildingRespVo;
    }

    @Override
    public void updateBuilding(BuildingUpdateReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Building>().eq("name", reqVo.getName())
                .ne("id", reqVo.getId()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Building building = new Building();
        BeanUtils.copyProperties(reqVo, building);
        baseMapper.updateById(building);
    }

    @Override
    public List<BuildingRespVo> BuildingList() {
        List<BuildingRespVo> buildingRespVos = new ArrayList<>();
        try {
            buildingRespVos = BeanCoperUtil.copyObjects(baseMapper.selectList(null), BuildingRespVo.class);
        } catch (Exception e) {
        }
        buildingRespVos.parallelStream().forEach(e -> {
            e.setUnitCount(baseMapper.findUnitCount(e.getId()));
            e.setHouseCount(baseMapper.findHouseCount(e.getId()));
            e.setCqyzCount(baseMapper.findCqyzCount(e.getId()));
            e.setRzyzCount(baseMapper.findRzyzCount(e.getId()));
            e.setRzzhCount(baseMapper.findRzzhCount(e.getId()));
        });
        return buildingRespVos;
    }

}
